package wty;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Random;

public class Administrator {
    public static JPanel panel;
    private JTable table; // 将 JTable 设为类的成员变量

    public Administrator() {
        init();
    }

    private void init() {
        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.setBackground(Color.lightGray);
        JLabel title = new JLabel("Welcome, Administrator!");
        title.setFont(new Font("", Font.PLAIN, 36));
        title.setForeground(Color.BLUE);
        title.setHorizontalAlignment(SwingConstants.CENTER);
        panel.add(title, BorderLayout.NORTH);

        // 获取数据模型
        DefaultTableModel model = Administrator_SQL.SQL();
        table = new JTable(model); // 初始化成员变量
        table.setFillsViewportHeight(true);//让表格高度根据窗口大小自动调整，填满
        table.setFont(new Font("SimSun", Font.PLAIN, 16));
        table.setRowHeight(30);


        // 创建表格的滚动面板
        JScrollPane scrollPane = new JScrollPane(table);
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());


        // 禁用按钮
        JButton BanButton = new JButton("Ban");
        buttonPanel.add(BanButton);
        BanButton.addActionListener(
                new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int selectedRow = table.getSelectedRow(); // 获取选中的行
                if (selectedRow != -1) { // 确保有选中行
                    String userId = table.getValueAt(selectedRow, 1).toString(); // 获取用户 ID
                    String newPassword = generateRandomPassword();
                    Administrator_SQL.updateUserPassword(userId, newPassword); // 更新数据库中的密码
                    table.setValueAt(newPassword, selectedRow, 2); // 更新表格中的密码显示
//                    System.out.println("用户 " + userId + " 的密码已更改为: " + newPassword);

                    //弹出对话框,以Administrator.panel的中心显示
                    JOptionPane.showMessageDialog(Administrator.panel, "用户 " + userId + " 的密码已更改为: " + newPassword+"(16位随机数)");
                    //提示成功

                } else {
                    JOptionPane.showMessageDialog(panel, "请先选择一个用户。");
                }
            }
        });


        //修改管理员密码按钮
        JButton ChangeButton = new JButton("Change Password");
        buttonPanel.add(ChangeButton);
        ChangeButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Administrator.panel.setVisible(false);
                        ChangeAdministratorPassword.panel.setVisible(true);
                    }
                }
        );



        // 返回按钮
        JButton BackButton = new JButton("Back");
        buttonPanel.add(BackButton);
        BackButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Administrator.panel.setVisible(false);
                Login.panel.setVisible(true);
            }
        });


        panel.add(buttonPanel, BorderLayout.SOUTH);
    }



    // 生成 15 位随机密码
    private String generateRandomPassword() {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        String password = "";
        Random random = new Random();

        for (int i = 0; i < 15; i++) {
            int randomIndex = random.nextInt(chars.length()); // 生成随机索引
            password += chars.charAt(randomIndex); // 获取字符并拼接到密码
        }

        return password;
    }


}
